package com.bsoft.wx.config;

import com.bsoft.wx.execption.EmosExection;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @program: emos-wx-api
 * @description: 异常类
 * @author: hdh
 * @create: 2023:02:02 15:42
 */

@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {
    @ResponseBody
    @ResponseStatus(HttpStatus.INSUFFICIENT_STORAGE)
    @ExceptionHandler(Exception.class)
    public String validExceptionHandler(Exception e) {
        log.info("执行异常", e);
        if (e instanceof MethodArgumentNotValidException) {
            MethodArgumentNotValidException exception = (MethodArgumentNotValidException) e;
            //将错误信息 返回给前台
            return exception.getBindingResult().getFieldError().getDefaultMessage();
        } else if (e instanceof EmosExection) {
            EmosExection exection = (EmosExection) e;
            return exection.getMsg();
        } else if (e instanceof UnauthorizedException) {
            return "你不具有相关的权限";
        } else {
            return "后端执行异常的错误";
        }
    }
}
